package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntityBiChannel;
import com.loongtech.bi.entity.system.EntityBiPlatform;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.core.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.List;

/**
 * 平台配置管理
 * 
 * @author marin
 * @date 2015-09-29
 */
@Controller("platformConfigAction")
@Scope("view")
public class PlatformConfigAction {
	@Autowired
	private BiPlatformManager biPlatformManager;
	
	private EntityBiPlatform entity;
	private List<EntityBiPlatform> list;
	
	@Autowired
	public void init() {
		initData();
	}
	
	private void initData() {
		entity = new EntityBiPlatform();
		list = BiPlatformManager.PLATFORM_LIST;
	}

	public void search() {
		List<Object> cons = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer(" from EntityBiPlatform l where 1=1 ");
		if (!Utils.isEmptyStr(entity.getId())) {
			hql.append(" and l.id = ? ");
			cons.add(entity.getId());
		}
		if (!Utils.isEmptyStr(entity.getName())) {
			hql.append(" and l.name = ? ");
			cons.add(entity.getName());
		}
		hql.append(" order by l.updateAt");
		
		list = biPlatformManager.findByQuery(hql.toString(), cons.toArray());
	}
	
	public void add() {
		FacesContext context = FacesContext.getCurrentInstance();
		if (validate(context, entity, true, list)) {
			biPlatformManager.persist(entity);
            context.addMessage(null, new FacesMessage("提示","添加成功！"));
			
			biPlatformManager.init();
			initData();  
		}
	}

	public void modify(EntityBiPlatform entity) {
		FacesContext context = FacesContext.getCurrentInstance();
		if (validate(context, entity, false, list)) {
			biPlatformManager.merge(entity);
			context.addMessage(null, new FacesMessage("修改成功！"));
			
			biPlatformManager.init();
			initData();
		}
	}
	
	public void delete(String id) {
		FacesContext context = FacesContext.getCurrentInstance();
		if (!Utils.isEmptyStr(id)) {
			List<EntityBiChannel> cl = BiPlatformManager.PLATFORM_CHANNELS_MAP.get(id);
			if (cl != null && cl.size() > 0) {
				context.addMessage(null, new FacesMessage("此平台与渠道有关联，不能删除！"));
				return;
			}
			
			biPlatformManager.removeById(id);
		}
		
		context.addMessage(null, new FacesMessage("删除成功！"));
		
		biPlatformManager.init();
		initData();
	}

	private boolean validate(FacesContext context, EntityBiPlatform ebp, boolean isAdd, List<EntityBiPlatform> ebpList) {
		if (Utils.isEmptyStr(ebp.getId())) {
			context.addMessage(null, new FacesMessage("提示", "平台ID不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(ebp.getName())) {
			context.addMessage(null, new FacesMessage("平台名称不能为空！"));
			return false;
		}
		if (isAdd) {  // 如果是添加，则ID与名称皆不能重
			for (EntityBiPlatform e : ebpList) {
				if (ebp.getId().equals(e.getId())) {
					context.addMessage(null, new FacesMessage("平台ID重复！"));
					return false;
				}
				if (ebp.getName().equals(e.getName())) {
					context.addMessage(null, new FacesMessage("平台名称重复！"));
					return false;
				}
			}
		} else {  // 如果是修改，则名称不能重
			for (EntityBiPlatform e : ebpList) {
				if (!ebp.getId().equals(e.getId()) && ebp.getName().equals(e.getName())) {
					context.addMessage(null, new FacesMessage("平台名称重复！"));
					return false;
				}
			}
		}
		
		return true;
	}
	
	public EntityBiPlatform getEntity() {
		return entity;
	}

	public void setEntity(EntityBiPlatform entity) {
		this.entity = entity;
	}

	public List<EntityBiPlatform> getList() {
		return list;
	}

	public void setList(List<EntityBiPlatform> list) {
		this.list = list;
	}
}
